﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Library.UserControls
{
    public partial class EditProcedureVersion : UserControl
    {
        Objects.ProcedureVersion _version;
        Objects.IProcedure _procedure;
        public EditProcedureVersion(Objects.ProcedureVersion version)
        {
            InitializeComponent();

            _version = version;
            _procedure = version.Parent;
            if (version.Parent.ProcVersionList.Count <= 0)
            {
                comboBox_Version.DropDownStyle = ComboBoxStyle.Simple;
                comboBox_Version.Text = version.Version;
            }
            else
            {
                foreach (var tmpver in version.Parent.ProcVersionList)
                    comboBox_Version.Items.Add(tmpver.Version);
                comboBox_Version.SelectedItem = version.Version;
            }
            textBox_User.Text = version.User;
            if (version.Project != null)
            {
                comboBox_Project.Items.Add(version.Project);
                comboBox_Project.SelectedItem = version.Project;
            }
            dateTimePicker_CreateDate.Value = version.Date;
            comboBox_Type.DataSource = Enum.GetValues(typeof(Objects.Procedure.EnumProcType));
            comboBox_Type.SelectedItem = version.Parent.Type;
        }

        public EditProcedureVersion(string MarkedText)
        {
            InitializeComponent();
        }

        public EditProcedureVersion(Objects.IProcedure procedure)
        {
            InitializeComponent();
            _procedure = procedure;
            if (procedure.ProcVersionList.Count <= 0)
                comboBox_Version.DropDownStyle = ComboBoxStyle.Simple;
            else
                foreach (var tmpver in procedure.ProcVersionList)
                    comboBox_Version.Items.Add(tmpver.Version);

            comboBox_Type.DataSource = Enum.GetValues(typeof(Objects.Procedure.EnumProcType));
            comboBox_Type.SelectedItem = Objects.Procedure.EnumProcType.Old;
        }

        public bool Save()
        {
            if (!string.IsNullOrWhiteSpace(comboBox_Version.Text))
            {
                Objects.Procedure.EnumProcType type = (Objects.Procedure.EnumProcType)comboBox_Type.SelectedItem;
                if (type == Objects.Procedure.EnumProcType.New && _version.Parent.Type != Objects.Procedure.EnumProcType.New)
                {
                    _version.Parent.Type = type;
                }

                _version.Version = comboBox_Version.Text.Trim();
                _version.User = textBox_User.Text.Trim();
                if (_version.Project != null && _version.Project.Project.Trim() != comboBox_Project.Text.Trim())
                    _version.Project = new Objects.ExternalProject(comboBox_Project.Text.Trim());
                _version.Date = dateTimePicker_CreateDate.Value;
                _version.Comment = richTextBox_Description.Text.Trim();
                return true;
            }
            return false;
        }

        public Objects.ProcedureVersion CreateNew()
        {
            if (!string.IsNullOrWhiteSpace(comboBox_Version.Text))
            {
                Objects.ProcedureVersion version = new Objects.ProcedureVersion(comboBox_Version.Text.Trim());
                version.Date = dateTimePicker_CreateDate.Value;
                version.Project = new Objects.ExternalProject(comboBox_Project.Text.Trim());
                version.User = textBox_User.Text.Trim();
                version.Comment = richTextBox_Description.Text.Trim();

                return version;
            }
            else
                return null;
        }

        private void comboBox_Version_SelectedIndexChanged(object sender, EventArgs e)
        {
            var ver = _procedure.ProcVersionList.FirstOrDefault(v => v.Version == comboBox_Version.Text);
            if (ver != null)
            {
                //comboBox_Version.Items.Clear();
                //foreach (var tmpver in ver.Parent.ProcVersionList)
                //    comboBox_Version.Items.Add(tmpver.Version);
                //comboBox_Version.Text = ver.Version;
                textBox_User.Text = ver.User;
                if (ver.Project != null && !comboBox_Project.Items.Contains(ver.Project))
                {
                    comboBox_Project.Items.Add(ver.Project);
                }
                if(comboBox_Project.Items.Contains(ver.Project))
                    comboBox_Project.SelectedItem = ver.Project;
                dateTimePicker_CreateDate.Value = ver.Date;
                comboBox_Type.DataSource = Enum.GetValues(typeof(Objects.Procedure.EnumProcType));
                comboBox_Type.SelectedItem = ver.Parent.Type; 
            }
        }
    }
}
